package com.googlecode.layout4j.web;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Converts layoutML to HTML (or any other markup).
 * @author Matt Humphreys
 */
public class RenderTag extends SimpleTagSupport {
	private String source;
	private JspFragment layoutStart;
	private JspFragment layoutEnd;
	private JspFragment rowStart;
	private JspFragment rowEnd;
	private JspFragment columnStart;
	private JspFragment columnEnd;
	private JspFragment contentStart;
	private JspFragment contentEnd;
	
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	
	public JspFragment getLayoutStart() {
		return layoutStart;
	}
	public void setLayoutStart(JspFragment layoutStart) {
		this.layoutStart = layoutStart;
	}
	public JspFragment getLayoutEnd() {
		return layoutEnd;
	}
	public void setLayoutEnd(JspFragment layoutEnd) {
		this.layoutEnd = layoutEnd;
	}
	
	public JspFragment getRowStart() {
		return rowStart;
	}
	public void setRowStart(JspFragment rowStart) {
		this.rowStart = rowStart;
	}
	public JspFragment getRowEnd() {
		return rowEnd;
	}
	public void setRowEnd(JspFragment rowEnd) {
		this.rowEnd = rowEnd;
	}
	
	public JspFragment getColumnEnd() {
		return columnEnd;
	}
	public void setColumnStart(JspFragment frag) {
		this.columnStart = frag;
	}
	public JspFragment getColumnStart() {
		return columnStart;
	}
	public void setColumnEnd(JspFragment columnEnd) {
		this.columnEnd = columnEnd;
	}
	
	public JspFragment getContentStart() {
		return contentStart;
	}
	public void setContentStart(JspFragment contentStart) {
		this.contentStart = contentStart;
	}
	public JspFragment getContentEnd() {
		return contentEnd;
	}
	public void setContentEnd(JspFragment contentEnd) {
		this.contentEnd = contentEnd;
	}
	
	@Override
	public void doTag() throws JspException { 
//		BodyContent bodyContent = getBodyContent();
//		Reader reader = bodyContent.getReader();
		Reader reader = new StringReader(source);
		
		PageContext pageContext = (PageContext) getJspContext(); 
		// Using print instead of write makes use of buffering
		//JspWriter out = pageContext.getOut();
		
		javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance();
	    spf.setValidating(false);
	    javax.xml.parsers.SAXParser sp;
		try {
			sp = spf.newSAXParser();
		} catch (ParserConfigurationException e) {
			throw new JspException(e);
		} catch (SAXException e) {
			throw new JspException(e);
		}
	    org.xml.sax.InputSource input = new InputSource(reader);
	    LayoutJspHandler handler = new LayoutJspHandler(pageContext);
	    handler.setLayoutStart(layoutStart);
	    handler.setLayoutEnd(layoutEnd);
	    handler.setRowStart(rowStart);
	    handler.setRowEnd(rowEnd);
	    handler.setColumnStart(columnStart);
	    handler.setColumnEnd(columnEnd);
	    handler.setContentStart(contentStart);
	    handler.setContentEnd(contentEnd);
	    
	    // Finally, tell the parser to parse the input and notify the handler
	    try {
			sp.parse(input, handler);
		} catch (SAXException e) {
			throw new JspException(e);
		} catch (IOException e) {
			throw new JspException(e);
		}
	}
}
